﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.DAL.LocalDataAccess/UserAccess
* 类       名: UserAccess
* 功      能:  TD
* 时      间:  2024-05-22 11:45:09
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using log4net.Util;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WaferSelectV3.Common;
using WaferSelectV3.DAL.DataEntity;
using WaferSelectV3.Extensions;
using WaferSelectV3.Models;
using WaferSelectV3.Services;

namespace WaferSelectV3.DAL.LocalDataAccess
{
    public class UserAccess
    {
        public static DataResult<List<UserEntity>> GetUsers()
        {
            DataResult<List<UserEntity>> result = new DataResult<List<UserEntity>>();
            try
            {
                using (var dataContext = new DataContext())
                {
                    result.Data = dataContext.UserInfos.ToList();
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }

            return result;
        }

        public static DataResult AddDataSingle(UserEntity entity)
        {
            DataResult result = new DataResult();
            try
            {
                using (var dataContext = new DataContext())
                {
                    dataContext.UserInfos.Add(entity);
                    int affectedRows = dataContext.SaveChanges();
                    if (affectedRows > 0)
                    {
                        result.State = true;
                    }
                    else
                    {
                        result.Message = ConstantProvider.DatabaseAddError;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        public static DataResult DataInit()
        {
            DataResult result = new DataResult();
            try
            {
                var dataInit = new List<UserEntity>()
                {
                    new UserEntity{UserName = "admin",Password= "admin",Role = (int)RoleType.工程师,
                        CreateBy="System",CreateTime=DateTimeHelper.GetTimeNowForSql()},
                };

                foreach (var entity in dataInit)
                {
                    var addResult = AddDataSingle(entity);
                    if (!addResult.State)
                    {
                        result = addResult;
                    }
                }
                result.State = true;

            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="newUser"></param>
        /// <returns></returns>
        public static DataResult UpdateDataSingle(UserEntity newUser)
        {
            DataResult result = new DataResult();
            try
            {
                using (var dataContext = new DataContext())
                {
                    var oldEntity = dataContext.UserInfos.FirstOrDefault(x => x.Id == newUser.Id);
                    if (oldEntity != null)
                    {
                        dataContext.Entry(oldEntity).CurrentValues.SetValues(newUser);
                        int affectedRows = dataContext.SaveChanges();
                        if (affectedRows < 0)
                        {
                            result.Message = ConstantProvider.DatabaseUpdateError;
                        }
                        else
                        {
                            result.State = true;
                        }
                    }
                    else
                    {
                        result.Message = $"要更新的数据不存在要更新的数据Id = {newUser.Id}";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        internal static DataResult DeleteSingleDataById(int id)
        {
            DataResult result = new DataResult();
            try
            {
                using (var dataContext = new DataContext())
                {
                    var entityDeleted = dataContext.UserInfos.FirstOrDefault(x => x.Id == id);
                    if (entityDeleted != null)
                    {
                        dataContext.Remove(entityDeleted);
                        int affectedRows = dataContext.SaveChanges();
                        if (affectedRows < 1)
                        {
                            result.Message = "数据参数失败";
                        }
                        else
                        {
                            result.State = true;
                        }
                    }
                    else
                    {
                        result.Message = $"要删除的数据Id不存在: {id}";
                    }
                };
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }
    }
}
